﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using WorkDAL.Meterials;
using Common;
using Lapas.Common;
using GrossControl;

namespace Material
{
    public partial class MaterialListForm : ListForm2
    {
        public MaterialListForm()
        {
            InitializeComponent();
        }

        protected override DataGridView Grid
        {
            get
            {
                return this.MainGrid;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ToolStripTextBox toolStripText = new ToolStripTextBox("tstxtMaterialId");
            ListFormToolStrip.Items.Insert(5, toolStripText);

            ToolStripButton item = new ToolStripButton("Tìm", Lapas.Common.Helper.GetBitmap("findIcon"));
            ListFormToolStrip.Items.Insert(6, item);
            this.ListFormToolStrip.Items[6].Click += new System.EventHandler(this.GetData);

        }

        private void GetData(object sender, EventArgs e)
        {
            this.GetData(ListFormToolStrip.Items[5].Text.Trim());
        }

        private void GetData(string materialId)
        {
            try
            {
                List<LiMaterial> liMaterial = MaterialDataAccess.Instance().Material_GetList(materialId);

                bindingSource.DataSource = liMaterial;
                MainGrid.DataSource = bindingSource;
                BindData();
                FormatBoldGrid();
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
                return;
            }
        }

        private void FormatBoldGrid()
        {
            FormatDataGrid.Instance().FormatBoldGrid(MainGrid, ColMaterialId.Name, string.Empty);
        }

        protected override void BindData()
        {
            base.BindData();
        }

        protected override void DisplayEditForm(EditForm.EditFormAction action)
        {
            MaterialEditForm materialEditForm = new MaterialEditForm();
            materialEditForm.FormAction = action;

            if (action == EditForm.EditFormAction.Change)
            {
                if (string.IsNullOrEmpty(((LiMaterial)bindingSource.Current).MaterialId))
                    return;
                
                materialEditForm.liMaterial = MaterialDataAccess.Instance().LiMaterial_GetById(((LiMaterial)bindingSource.Current).MaterialId);
            }

            materialEditForm.ShowDialog();
            if (materialEditForm.Reload)
            {
                this.GetData(ListFormToolStrip.Items[5].Text.Trim());
            }
        }


        //protected override void OnGross()
        //{
        //    GrossTableForm grossTableForm = new GrossTableForm();
        //    grossTableForm.GrossKey = "MetarialGroup";
        //}

    }
}
